package ltd.newbee.mall.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.dao.DictDataMapper;
import ltd.newbee.mall.dao.DictTypeMapper;
import ltd.newbee.mall.entity.DictData;
import ltd.newbee.mall.entity.DictType;
import ltd.newbee.mall.entity.query.DictQuery;
import ltd.newbee.mall.util.PageResult;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author hjchen
 * @since 2023-03-19
 */
@RestController
@RequestMapping("/manage-api/v1/dict")
public class DictController {

    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @GetMapping("/type/{dictType}")
    public Result dictTypes(@PathVariable String dictType) {
        System.out.println("dictType=" + dictType);
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DictType::getDictCode, dictType);
        return ResultGenerator.genSuccessResult(dictTypeMapper.selectList(queryWrapper));
    }

    @PostMapping("/type")
    public Result saveDict(@RequestBody DictType dictType) {
        System.out.println("dictType=" + dictType);
        dictType.preInsert();
        dictTypeMapper.insert(dictType);
        return ResultGenerator.genSuccessResult(ServiceResultEnum.SUCCESS.getResult());
    }

    @PutMapping("/type")
    public Result updateDict(@RequestBody DictType dictType) {
        System.out.println("dictType=" + dictType);
        dictType.preInsert();
        dictTypeMapper.updateById(dictType);
        return ResultGenerator.genSuccessResult(ServiceResultEnum.SUCCESS.getResult());
    }

    @PostMapping("/data")
    public Result save(@RequestBody DictData dictData) {
        System.out.println("dictData=" + dictData);
        dictData.preInsert();
        dictDataMapper.insert(dictData);
        return ResultGenerator.genSuccessResult(ServiceResultEnum.SUCCESS.getResult());
    }

    @PutMapping("/data")
    public Result update(@RequestBody DictData dictData) {
        System.out.println("dictType=" + dictData);
        dictData.preInsert();
        dictDataMapper.updateById(dictData);
        return ResultGenerator.genSuccessResult(ServiceResultEnum.SUCCESS.getResult());
    }

    @GetMapping("/typeId/{id}")
    public Result dictId(@PathVariable String id) {
        System.out.println("dictId=" + id);
        return ResultGenerator.genSuccessResult(dictTypeMapper.selectById(id));
    }

    @GetMapping("/dataId/{id}")
    public Result dataId(@PathVariable String id) {
        System.out.println("dictId=" + id);
        return ResultGenerator.genSuccessResult(dictDataMapper.selectById(id));
    }

    @GetMapping("/listType")
    public Result listType(DictQuery query) {
        System.out.println("param \n" + JSONUtil.toJsonStr(query));
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StringUtils.isNotEmpty(query.getDictCode()), DictType::getDictCode, query.getDictCode());
        queryWrapper.eq(StringUtils.isNotEmpty(query.getDictName()), DictType::getDictName, query.getDictCode());
        queryWrapper.between(!StringUtils.isAnyEmpty(query.getBeginTime(), query.getEndTime()), DictType::getCreateTime, query.getBeginTime(), query.getEndTime());
        Page page = dictTypeMapper.selectPage(new Page<>(query.getPageNumber(), query.getPageSize()), queryWrapper);
        return ResultGenerator.genSuccessResult(new PageResult(page.getRecords(), (int) page.getTotal(), query.getPageSize(), query.getPageNumber()));

    }


    @GetMapping("/listData")
    public Result listData(DictQuery query) {
        System.out.println("param \n" + JSONUtil.toJsonStr(query));
        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StrUtil.isNotEmpty(query.getDictId()), DictData::getDictId, query.getDictId());
        Page page = dictDataMapper.selectPage(new Page<>(query.getPageNumber(), query.getPageSize()), queryWrapper);
        return ResultGenerator.genSuccessResult(new PageResult(page.getRecords(), (int) page.getTotal(), query.getPageSize(), query.getPageNumber()));
    }


}
